package uiautomator;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Instrumentation;
import android.app.UiAutomation;
import android.content.Context;
import android.os.Build;
import android.os.HandlerThread;

import androidx.test.uiautomator.UiAutomationHelper;

import mirror.android.app.UiAutomationConnection;
import mirror.android.app.UiAutomationM;
import proxy.Bridge;

/* loaded from: classes2.dex */
public class InstrumentShellWrapper extends Instrumentation {
    private static final long CONNECT_TIMEOUT_MILLIS = 5000;
    private static final String HANDLER_THREAD_NAME = "UiAutomatorHandlerThread";
    private static InstrumentShellWrapper sInstance;
    private final HandlerThread mHandlerThread;
    private UiAutomation mUiAutomation;
    private Object mUiAutomationConnection;

    private InstrumentShellWrapper() {
        HandlerThread handlerThread = new HandlerThread(HANDLER_THREAD_NAME);
        this.mHandlerThread = handlerThread;
        this.mUiAutomationConnection = UiAutomationConnection.ctor.newInstance();
        handlerThread.start();
    }

    public static InstrumentShellWrapper getInstance() {
        if (sInstance == null) {
            sInstance = new InstrumentShellWrapper();
        }
        return sInstance;
    }

    @Override // android.app.Instrumentation
    public Context getContext() {
        return Bridge.getInstance().getContext();
    }

    @Override // android.app.Instrumentation
    public Context getTargetContext() {
        return Bridge.getInstance().getContext();
    }

    public void destroy() {
        if (!this.mHandlerThread.isAlive()) {
            throw new IllegalStateException("Already disconnected!");
        }
        this.mHandlerThread.quit();
    }

    public void setCompressedLayoutHierarchy(boolean compressed) {
        AccessibilityServiceInfo info = this.mUiAutomation.getServiceInfo();
        if (compressed) {
            info.flags &= -3;
        } else {
            info.flags |= 2;
        }
        this.mUiAutomation.setServiceInfo(info);
    }

    @Override // android.app.Instrumentation
    public UiAutomation getUiAutomation(int flags) {
        boolean mustCreateNewAutomation = this.mUiAutomation == null || mirror.android.app.UiAutomation.isDestroyed.call(this.mUiAutomation, new Object[0]).booleanValue();
        if (this.mUiAutomationConnection != null) {
            if (!mustCreateNewAutomation && mirror.android.app.UiAutomation.getFlags.call(this.mUiAutomation, new Object[0]).intValue() == flags) {
                return this.mUiAutomation;
            }
            if (mustCreateNewAutomation) {
                this.mUiAutomation = mirror.android.app.UiAutomation.ctor.newInstance(this.mHandlerThread.getLooper(), this.mUiAutomationConnection);
            } else {
                mirror.android.app.UiAutomation.disconnect.call(this.mUiAutomation, new Object[0]);
            }
            if (Build.VERSION.SDK_INT > 30) {
                try {
                    mirror.android.app.UiAutomation.connectWithTimeout.call(this.mUiAutomation, Integer.valueOf(flags), Long.valueOf(CONNECT_TIMEOUT_MILLIS));
                    return this.mUiAutomation;
                } catch (Exception e) {
                    mirror.android.app.UiAutomation.destroy.call(this.mUiAutomation, new Object[0]);
                    this.mUiAutomation = null;
                }
            } else {
//                UiAutomationReflectHelper helper=new UiAutomationReflectHelper();
//                helper.connect(this.mUiAutomation,flags);
//                mirror.android.app.UiAutomation.connect.call(this.mUiAutomation, Integer.valueOf(flags));
//                UiAutomationHelper.invokeConnect(this.mUiAutomation, flags);
                return this.mUiAutomation;
            }
        }
        return null;

//        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
//        return uiAutomation;
    }

    @Override // android.app.Instrumentation
    public UiAutomation getUiAutomation() {
        UiAutomation uiAutomation = this.mUiAutomation;
        if (uiAutomation != null) {
            return uiAutomation;
        }
        this.mUiAutomation = UiAutomationM.ctor.newInstance(this.mHandlerThread.getLooper(), this.mUiAutomationConnection);
        UiAutomationM.connect.call(this.mUiAutomation, new Object[0]);
        return this.mUiAutomation;
    }
}